package com.rtx.wms.customer.k3.base.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rtx.wms.customer.k3.base.service.AbsBasicSchedulerAPI;

@Component
public class CustomerHandle extends AbsBasicSchedulerAPI{
	
	Logger log = LoggerFactory.getLogger(this.getClass());
	
	@Override
	protected String getConfigCode() {
		return "wms-base-customer";
	}

	@Override
	protected String getK3FormId() {
		return "BD_Customer";
	}

	@Override
	protected String getK3Fields() {
		return "FCUSTID,FDOCUMENTSTATUS,FFORBIDSTATUS,FNAME,FNUMBER,FDESCRIPTION,FCREATEORGID,FUSEORGID,FCREATORID,FMODIFIERID,FCREATEDATE,FMODIFYDATE,FSHORTNAME,FCOUNTRY,FPROVINCIAL,FZIP,FTEL,FTAXREGISTERCODE,FFAX,FGROUPCUSTID,FSUPPLIERID,FTRADINGCURRID,FSALDEPTID,FSALGROUPID,FSELLER,FTRANSLEADTIME,FPRICELISTID,FDISCOUNTLISTID,FSETTLETYPEID,FRECEIVECURRID,FRECCONDITIONID,FISCREDITCHECK,FAPPROVERID,FAPPROVEDATE,FFORBIDDERID,FFORBIDDATE,FTAXTYPE,FCUSTTYPEID,FADDRESS,FWEBSITE,FCOMPANYSCALE,FCOMPANYCLASSIFY,FCOMPANYNATURE,FCORRESPONDORGID,FPRIORITY,FINVOICETYPE,FTAXRATE,FISDEFPAYER,FCPADMINCODE,FISGROUP,FINVOICETITLE,FINVOICEBANKNAME,FINVOICEBANKACCOUNT,FINVOICETEL,FINVOICEADDRESS,FISTRADE,FUNCHECKEXPECTQTY,FISGROUPTAG,FLEGALPERSON,FREGISTERFUND,FFOUNDDATE,FDOMAINS,FSOCIALCRECODE,FREGISTERADDRESS,F_FSCA_COMBO,F_FSCA_TEXT,F_FSCA_ASSISTANT,F_FSCA_TEXT1";
	}

	@Override
	protected String getWmsAPIMappingCode() {
		return "k3_wms_customer_create";
	}

	@Override
	protected String queryBasicData(int startRow, int topRowCount,int max) throws Exception {
		String response = super.queryBasicData(startRow, topRowCount,max);
		List<JSONObject> list = JSON.parseArray(response, JSONObject.class);
		for(JSONObject customer:list) {
			String FCUSTID = customer.getString("FCUSTID");
			String contact = concatRequest(FCUSTID);
			List<JSONObject> contactArray = JSONArray.parseArray(contact,JSONObject.class);
			if(contactArray == null || contactArray.size()<1) {
				customer.put("CONTACT", new HashMap<>());
			}else {
				customer.put("CONTACT", contactArray.get(0));
			}
		}
		return JSON.toJSONString(list);
	}

	private String concatRequest(String fcustid) throws Exception {
		if(!StringUtils.hasLength(fcustid)) {
			return "[]";
		}
		Map<String,Object> request = new JSONObject();
		request.put("FormId", "BD_CommonContact");
		request.put("FieldKeys", "FDOCUMENTSTATUS,FFORBIDSTATUS,FNAME,FNUMBER,FMODIFIERID,FCREATORID,FCREATEDATE,FMODIFYDATE,FDESCRIPTION,FPOST,FCOMPANYTYPE,FCOMPANY,FTEL,FMOBILE,FFAX,FEMAIL,FBIZLOCATION,FBIZLOCNUMBER,FBIZADDRESS,FISDEFAULTCONSIGNEE,FISDEFAULTCONTACT,FFORBIDDERID,FFORBIDDATE,FCUSTID");
		
		JSONArray filter = new JSONArray();
		JSONObject filter1 = new JSONObject();
		filter1.put("Left", "("); 
		filter1.put("FieldName", "FCUSTID"); 
		filter1.put("Compare", "="); 
		filter1.put("Value", fcustid); 
		filter1.put("Right", ")"); 
		filter1.put("Logic", ""); 
		filter.add(filter1);
		
		request.put("FilterString", filter);
		request.put("OrderString", "");
		request.put("TopRowCount", "10");
		request.put("StartRow", "1");
		request.put("Limit", "10");
		request.put("SubSystemId", "");
		String res = k3Api.getK3API().billQuery(JSON.toJSONString(request));
		return res;
	}
	
}
